package com.jhte.scheduleJob.base.util;

import java.security.MessageDigest;

/**
 * Created by Administrator on 2017/9/25 0025.
 */
public class MD5 {
    public static String MD5(String source)  {
        /*try {
            byte[] bytesOfMessage = s.getBytes("GBK");
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(bytesOfMessage);
            String password=new String(thedigest);
            return password;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";*/
        return MD5(source,false);
    }

    /**
     * 根据传入字符串 返回Md5字符串
     * @param source	源字符串
     * @param lowercase 是否以小写字母的形式返回
     * @return
     */
    public static String MD5(String source,boolean lowercase) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

        try {
            byte[] btInput = source.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            String res = new String(str);
            return lowercase?res.toLowerCase():res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*public static void main(String[] args) {
        String password = MD5("Admin123456");
        System.out.print(password);
    }*/

    /*public static void main(String[] args) {
        *//*String sql = "'0000007863','0000007352','0000007351','0000007355'";
        Map map = new HashMap();
        map.put("drugCode", sql);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String str = objectMapper.writeValueAsString(map);
            System.out.println(str);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }*//*

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("before java8");
            }
        }).start();

        new Thread(() -> System.out.println("java8")).start();
    }*/

}
